| Conditions | 1 |
| Paths | 1 |
| Total Lines | 108 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import ServersService from '../../../src/servers/services/ServersService'; |
||
| 3 | describe('ServersService', () => { |
||
| 4 | const servers = { |
||
| 5 | abc123: { id: 'abc123' }, |
||
| 6 | def456: { id: 'def456' }, |
||
| 7 | }; |
||
| 8 | const createStorageMock = (returnValue) => ({ |
||
| 9 | set: jest.fn(), |
||
|
|
|||
| 10 | get: jest.fn(() => returnValue), |
||
| 11 | }); |
||
| 12 | |||
| 13 | describe('listServers', () => { |
||
| 14 | it('returns an empty object when servers are not found in storage', () => { |
||
| 15 | const storageMock = createStorageMock(); |
||
| 16 | const service = new ServersService(storageMock); |
||
| 17 | |||
| 18 | const result = service.listServers(); |
||
| 19 | |||
| 20 | expect(result).toEqual({}); |
||
| 21 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 22 | expect(storageMock.set).not.toHaveBeenCalled(); |
||
| 23 | }); |
||
| 24 | |||
| 25 | it('returns value from storage when found', () => { |
||
| 26 | const storageMock = createStorageMock(servers); |
||
| 27 | const service = new ServersService(storageMock); |
||
| 28 | |||
| 29 | const result = service.listServers(); |
||
| 30 | |||
| 31 | expect(result).toEqual(servers); |
||
| 32 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 33 | expect(storageMock.set).not.toHaveBeenCalled(); |
||
| 34 | }); |
||
| 35 | }); |
||
| 36 | |||
| 37 | describe('findServerById', () => { |
||
| 38 | it('returns undefined when requested server is not found', () => { |
||
| 39 | const storageMock = createStorageMock(servers); |
||
| 40 | const service = new ServersService(storageMock); |
||
| 41 | |||
| 42 | const result = service.findServerById('ghi789'); |
||
| 43 | |||
| 44 | expect(result).toBeUndefined(); |
||
| 45 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 46 | expect(storageMock.set).not.toHaveBeenCalled(); |
||
| 47 | }); |
||
| 48 | |||
| 49 | it('returns server from list when found', () => { |
||
| 50 | const storageMock = createStorageMock(servers); |
||
| 51 | const service = new ServersService(storageMock); |
||
| 52 | |||
| 53 | const result = service.findServerById('abc123'); |
||
| 54 | |||
| 55 | expect(result).toEqual({ id: 'abc123' }); |
||
| 56 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 57 | expect(storageMock.set).not.toHaveBeenCalled(); |
||
| 58 | }); |
||
| 59 | }); |
||
| 60 | |||
| 61 | describe('createServer', () => { |
||
| 62 | it('adds one server to the list', () => { |
||
| 63 | const storageMock = createStorageMock(servers); |
||
| 64 | const service = new ServersService(storageMock); |
||
| 65 | |||
| 66 | service.createServer({ id: 'ghi789' }); |
||
| 67 | |||
| 68 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 69 | expect(storageMock.set).toHaveBeenCalledTimes(1); |
||
| 70 | expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { |
||
| 71 | abc123: { id: 'abc123' }, |
||
| 72 | def456: { id: 'def456' }, |
||
| 73 | ghi789: { id: 'ghi789' }, |
||
| 74 | }); |
||
| 75 | }); |
||
| 76 | }); |
||
| 77 | |||
| 78 | describe('createServers', () => { |
||
| 79 | it('adds multiple servers to the list', () => { |
||
| 80 | const storageMock = createStorageMock(servers); |
||
| 81 | const service = new ServersService(storageMock); |
||
| 82 | |||
| 83 | service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]); |
||
| 84 | |||
| 85 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 86 | expect(storageMock.set).toHaveBeenCalledTimes(1); |
||
| 87 | expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { |
||
| 88 | abc123: { id: 'abc123' }, |
||
| 89 | def456: { id: 'def456' }, |
||
| 90 | ghi789: { id: 'ghi789' }, |
||
| 91 | jkl123: { id: 'jkl123' }, |
||
| 92 | }); |
||
| 93 | }); |
||
| 94 | }); |
||
| 95 | |||
| 96 | describe('deleteServer', () => { |
||
| 97 | it('removes one server from the list', () => { |
||
| 98 | const storageMock = createStorageMock(servers); |
||
| 99 | const service = new ServersService(storageMock); |
||
| 100 | |||
| 101 | service.deleteServer({ id: 'abc123' }); |
||
| 102 | |||
| 103 | expect(storageMock.get).toHaveBeenCalledTimes(1); |
||
| 104 | expect(storageMock.set).toHaveBeenCalledTimes(1); |
||
| 105 | expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), { |
||
| 106 | def456: { id: 'def456' }, |
||
| 107 | }); |
||
| 108 | }); |
||
| 109 | }); |
||
| 110 | }); |
||
| 111 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.